package com.pda.yuanjing.pages.sybom;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.CodeBomBeanOut;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.message.MessageBus;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.dialogs.ChooseDialog;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import com.public_module.tools.VoiceUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class bomQueryActivity extends BaseActivity<PageModel, PageView, PagePresent> implements PageView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.lll)
    LinearLayout lll;
    @BindView(R.id.tvv)
    TextView tvv;
    @BindView(R.id.listView)
    ListView listView;

    private String TAG = "bomQueryActivity";
    CodeBomBeanOut codeBomBeanOutm = null;
    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    ItemAdapter itemAdapter;
    ChooseDialog chooseDialog1;
    String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bom);
        ButterKnife.bind(this);
        tvTitle.setText("溯源BOM " + SharedPreferencesManger.getRealName());
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        String ss = StringProcess.codeMat(string);
                        presenter.query(ss,"");
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        EventBus.getDefault().register(this);
        voiceUtils = new VoiceUtils(this);

        chooseDialog1 = new ChooseDialog(bomQueryActivity.this, "是否删除数据", R.style.CommentDialog, new ChooseDialog.PriorityListener() {
            @Override
            public void setActivityText() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chooseDialog1.dismiss();
                        presenter.del(id);
                    }
                });
            }
        });
        WindowManager.LayoutParams lp1 = chooseDialog1.getWindow().getAttributes();
        lp1.gravity = Gravity.CENTER;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
        EventBus.getDefault().unregister(this);
    }

    @Override
    public PageModel createModel() {
        return new PageModelImpl(this);
    }

    @Override
    public PageView createView() {
        return this;
    }

    @Override
    public PagePresent createPresenter() {
        return new PagePresent();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //oet.setText("YK21AUG23CD2C7352");
        //presenter.query(oet.getText().toString(),"");
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
                tvv.setText("");
                listView.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void tipS(CodeBomBeanOut codeBomBeanOut) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeBomBeanOutm = codeBomBeanOut;
                tvv.setText(codeBomBeanOut.getData().getProductName());
                listView.setVisibility(View.VISIBLE);
                itemAdapter = new ItemAdapter(bomQueryActivity.this, codeBomBeanOut.getData().getList(),1);
                listView.setAdapter(itemAdapter);
            }
        });
    }

    @Override
    public void del(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showMsg(str);
                presenter.query(oet.getText().toString(),"");
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBaseMessageEvent(MessageBus messageBus) {
        if (messageBus.getValue() == 0) {
            codeBomBeanOutm.getData().getList().get(messageBus.getIndex()).setProductID(codeBomBeanOutm.getData().getProductID());
            intentToActivityWithSerializableParameter(this, BomBJActivity.class, PDApp.process, codeBomBeanOutm.getData().getList().get(messageBus.getIndex()));
        } else if (messageBus.getValue() == 1) {
            id = codeBomBeanOutm.getData().getList().get(messageBus.getIndex()).getBomID();
            chooseDialog1.show();
        }
    }

    @OnClick({R.id.iv_title_left, R.id.bt, R.id.iv_title_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                presenter.query(oet.getText().toString(),"");
                break;
            case R.id.iv_title_right:
//                if (codeBomBeanOutm == null) {
//                    showMsg("当前没有bom数据");
//                    return;
//                }
                CodeBomBeanOut.DataDTO.ListDTO listDTO = new CodeBomBeanOut.DataDTO.ListDTO();
                //listDTO.setProductID(codeBomBeanOutm.getData().getProductID());
                intentToActivityWithSerializableParameter(this, BomBJActivity.class, PDApp.process, listDTO);
                break;
        }
    }
}
